﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;

namespace WebSite.admin.link
{
    public partial class link : BaseAdmin
    {


        // Methods
        private void bind()
        {
            string sql = "select * from DB_Link where languages=" + info.languages("") + " order by linkorder";
            this.GridView1.DataSource = SQLAccess.GreatDs(sql);
            this.GridView1.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i <= (this.GridView1.Rows.Count - 1); i++)
            {
                CheckBox box = (CheckBox)this.GridView1.Rows[i].FindControl("CheckBox1");
                if (box.Checked)
                {
                    SQLAccess.DoSql("DELETE FROM DB_Link where linkid=" + this.GridView1.DataKeys[i].Value + "");
                }
            }
            base.Response.Write("<script language=javascript>alert('删除成功!');window.location.href='link.aspx';</script>");
            base.Response.End();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            for (int i = 0; i <= (this.GridView1.Rows.Count - 1); i++)
            {
                CheckBox box = (CheckBox)this.GridView1.Rows[i].FindControl("CheckBox1");
                TextBox box2 = (TextBox)this.GridView1.Rows[i].FindControl("TextBox1");
                if (box.Checked)
                {
                    SQLAccess.DoSql(string.Concat(new object[] { "update DB_Link set linkorder=", re.replace(box2.Text), "  where linkid=", this.GridView1.DataKeys[i].Value, "" }));
                }
            }
            base.Response.Write("<script language=javascript>alert('更新成功!');window.location.href='link.aspx';</script>");
            base.Response.End();
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            string sql = "select * from DB_Link where languages=" + info.languages("") + "";
            if (re.replace(this.keyname.Text) != "")
            {
                sql = sql + " and linkname like '%" + re.replace(this.keyname.Text) + "%' ";
            }
            sql = sql + " order by linkorder";
            this.GridView1.DataSource = SQLAccess.GreatDs(sql);
            this.GridView1.DataBind();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            this.bind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if ((e.Row.RowType == DataControlRowType.Header) || (e.Row.RowType == DataControlRowType.DataRow))
            {
                CheckBox box = new CheckBox();
                box = (CheckBox)e.Row.FindControl("CheckBox1");
                if (box != null)
                {
                    if (e.Row.RowType == DataControlRowType.DataRow)
                    {
                        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=\"" + e.Row.Style["BACKGROUND-COLOR"] + "\"");
                        e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor=\"#efefef\"");
                    }
                    else if (e.Row.RowType == DataControlRowType.Header)
                    {
                        box.Attributes.Add("onclick", "SelectAll(this.checked)");
                    }
                }
            }
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            SQLAccess.DoSql("DELETE FROM DB_Link where linkid=" + ((int)this.GridView1.DataKeys[e.RowIndex].Value));
            this.bind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.Button1.Attributes["onclick"] = "return CheckSelect(true,'del');";
            this.Button2.Attributes["onclick"] = "return CheckSelect(true,'update');";
            if (!base.IsPostBack)
            {
                this.bind();
            }
        }


    }
}